package com.oktest.measure.entity

import androidx.annotation.Keep
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.chad.library.adapter.base.entity.node.BaseExpandNode
import com.chad.library.adapter.base.entity.node.BaseNode
import com.oktest.measure.MyApp

/**
 * @Author hhh
 * @Date 2020/10/24
 */

@Keep
@Entity(tableName = "Xml")
data class Xml(
    @PrimaryKey var id: Long = 0,
    var name: String = "",
    var state: String = "已完成" // 调试状态
)

@Keep
@Entity(tableName = "Device")
data class Device(
    @PrimaryKey var id: Long = 0,
    var name: String = "",
    var rate: String = "",
    var xmlId: Long = 0
) : BaseExpandNode() {
    override val childNode: MutableList<BaseNode>
        get() {
            val list = arrayListOf<BaseNode>()
            list.addAll(MyApp.roomDatabase.deviceDao().types(id))
            return list
        }
}

@Keep
@Entity(tableName = "Type")
data class Type(
    @PrimaryKey var id: Long = 0,
    var name: String = "",
    var type: String = "",
    var deviceId: Long = 0,
    var isChecked: Boolean = false,
    var isOpen: Boolean = false,
) : BaseExpandNode() {
    override val childNode: MutableList<BaseNode>?
        get() {
            val list = arrayListOf<BaseNode>()
            list.addAll(MyApp.roomDatabase.typeDao().measures(id))
            return list
        }

    fun isDone(): Boolean {
        val measureList: List<Measure> = MyApp.roomDatabase.typeDao().measures(id)
        for (measure in measureList) {
            if (!measure.isSuccessful) {
                return false
            }
        }
        return true
    }
}

@Keep
@Entity(tableName = "Measure")
data class Measure(
    @PrimaryKey var id: Long = 0,
    var value: String = "",
    var angle: String = "",
    var sc: String = "",
    var sv: String = "",
    var typeId: Long = 0,   // meterage 的id
    var isSuccessful: Boolean = false,
    var isDebugging: Boolean = false,
    var isChecked: Boolean = false,
    var errorMsg: String = "", // 对量结果
    var time: String = "",
    var rate: String = "" // 变比
) : BaseNode() {
    override val childNode: MutableList<BaseNode>?
        get() = null

    fun parentType(): Type {
        return MyApp.roomDatabase.measureDao().type(typeId)
    }

    fun getValueDisplay(): String {
        return if (parentType().childNode?.size == 1) {
            "上送"
        } else {
            if (value == "1") {
                "合"
            } else {
                "闭"
            }
        }
    }

    private fun signValue(): String {
        return if (parentType().childNode?.size == 1) {
            "1"
        } else {
            if (value == "1") {
                "1"
            } else {
                "0"
            }
        }
    }

    // 是否遥测
    fun isTelemetry() = parentType().type == "1"

    // 测量值
    fun getMeasureValue() = if(isTelemetry()) scv() else signValue()

    fun scv(): String {
        return if(this.sc.isNotEmpty()) this.sc else this.sv
    }
}